﻿using System;
using System.ServiceProcess;
using System.Threading;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.SG;
using Biz.Process;
using Hjt212Parser;
using log4net;

namespace Biz
{
    public partial class BizProcessor : ServiceBase
    {
        private ILog log = LogManager.GetLogger(typeof(BizProcessor));
        int milliSeconds = 10;

        public BizProcessor()
        {
            InitializeComponent();
        }
        //test
        public void Main()
        {
            OnStart(null);
        }
        protected override void OnStart(string[] args)
        {
            //启动各路线程
            new Thread(ProcessData).Start();
            new Thread(Process3071).Start();
            new Thread(Process901X).Start();
            new Thread(Process308X).Start();
            new Thread(Process0000).Start();
            new Thread(Process9999).Start();
            new Thread(ProcessDownCmds).Start();
            log.Info("各路线程启动完成...");
        }
        protected override void OnStop()
        {
            log.Info("The processor Stoped!");
        }
        private void ProcessData(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("2011");
                    new CommonProcessor().Process("2051");
                    new CommonProcessor().Process("2061");
                    new CommonProcessor().Process("2031");
                }
                catch (Exception ex)
                {
                    log.Error( ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void Process3071(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("3071");
                }
                catch (Exception ex)
                {
                    log.Error( ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void Process901X(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("9011");
                    new CommonProcessor().Process("9012");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void Process308X(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("3081");
                    new CommonProcessor().Process("3082");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void Process0000(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("0000");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void Process9999(object state)
        {
            while (true)
            {
                try
                {
                    new CommonProcessor().Process("9999");
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }

        private void ProcessDownCmds(object state)
        {
            while (true)
            {
                try
                {
                    using (var gCtx = new GatewayMonitorContext())
                    using (var pCtx = new PlatformXpfContext())
                    {
                        ProcessorDownline.GenDownlineCmdToGateway(gCtx,pCtx);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }

    }
}
